<script setup>
import {
  Management,
  ArrowDownBold,
  SwitchButton,
} from "@element-plus/icons-vue";
import router from "@/router";
import { ElMessage } from "element-plus";
import { reactive } from "vue";
import { getToken, removeToken } from "@/utils/cookie";
import { getUserInfoService, logoutService } from "@/api/suser";
const token = getToken();
const loginUser = reactive({
  nickName: "",
});
async function getUserInfo() {
  try {
    const userInfo = await getUserInfoService();
    loginUser.nickName = userInfo.data.nickName;
  } catch (error) {
    console.error("获取用户信息失败:", error);
    ElMessage.error("获取用户信息失败");
  }
}
getUserInfo();
async function handleLogout() {
  try {
    await ElMessageBox.confirm("确认退出", "温馨提示", {
      confirmButtonText: "确认",
      cancelButtonText: "退出",
      type: "warning",
    });
    await logoutService();
    ElMessage.success("退出登录成功");
    removeToken();
    router.push("/oj/login");
  } catch (error) {
    console.error("退出登录失败:", error);
  }
}
</script>
<template>
  <el-container class="layout-container">
    <el-container>
      <el-header class="el-header">
        <el-dropdown placement="bottom-end">
          <span class="el-dropdown__box">
            <div><strong>当前用户：</strong>{{ loginUser.nickName }}</div>
            <el-icon>
              <ArrowDownBold />
            </el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item :icon="SwitchButton" @click="handleLogout"
                >退出登录</el-dropdown-item
              >
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </el-header>
      <el-main class="layout-bottom-box">
        <div class="left">
          <el-aside width="200px" class="el-aside">
            <el-menu class="el-menu" router>
              <el-menu-item index="/oj/layout/user">
                <el-icon>
                  <Management />
                </el-icon>
                <span>用户管理</span>
              </el-menu-item>
              <el-menu-item index="/oj/layout/question">
                <el-icon>
                  <Management />
                </el-icon>
                <span>题目管理</span>
              </el-menu-item>
              <el-menu-item index="/oj/layout/competition">
                <el-icon>
                  <Management />
                </el-icon>
                <span>竞赛管理</span>
              </el-menu-item>
              <el-menu-item index="/oj/layout/test">
                <el-icon>
                  <Management />
                </el-icon>
                <span>test</span>
              </el-menu-item>
            </el-menu>
          </el-aside>
        </div>
        <div class="right">
          <RouterView />
        </div>
      </el-main>
    </el-container>
  </el-container>
</template>

<style lang="scss" scoped>
.layout-container {
  height: 100vh;
  background: #f0f4f8;

  .layout-bottom-box {
    display: flex;
    justify-content: space-between;
    height: calc(100vh - 60px);
    overflow: hidden;

    .left {
      margin-right: 20px;
      background: #fff;
      display: flex;
      box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);

      :deep(.el-menu) {
        flex: 1;
        border-right: none;

        .el-menu-item {
          border-radius: 8px;
          margin: 4px 8px;

          &.is-active {
            background-color: #e6f0ff;
            color: #2a6fc7;
            border-right: 3px solid #2a6fc7;
          }

          &:hover {
            background-color: #f0f7ff;
            color: #2a6fc7;
          }
        }
      }
    }

    .right {
      flex: 1;
      overflow-y: auto;
      background: #fff;
      padding: 20px;
      box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
      border-radius: 8px;
    }
  }

  .el-aside {
    background-color: #fff;
    border-right: 1px solid #e6e8eb;
  }

  .el-header {
    background-color: #2a6fc7;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    height: 60px;
    padding: 0 20px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);

    .el-dropdown__box {
      display: flex;
      align-items: center;
      color: #fff;
      cursor: pointer;
      padding: 8px 16px;
      border-radius: 6px;
      transition: all 0.3s ease;

      &:hover {
        background-color: rgba(255, 255, 255, 0.1);
      }

      div {
        color: #fff;
        font-size: 14px;
      }

      .el-icon {
        color: #fff;
        margin-left: 8px;
        font-size: 12px;
      }

      &:active,
      &:focus {
        outline: none;
      }
    }
  }
}
</style>
